package gamejam.game;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;

public class Player {
	
	private int xLoc, yLoc, width, height, jumpstart;
	private boolean runningLeft = false, runningRight = false, jumping = false, falling = false;
	private Rectangle2D top, left, right, bottom;
	private Map m;
	
	public Player(Map m){
		this.m = m;
		setPosition(m.getStartPosition());
		width = Map.TILE_SIZE;
		height = Map.TILE_SIZE;
		top = new Rectangle2D.Double();
		left = new Rectangle2D.Double();
		right = new Rectangle2D.Double();
		bottom = new Rectangle2D.Double();
		updateRects();
	}
	public int getCenterX(){
		return xLoc+(width/2);
	}
	public int getCenterY(){
		return yLoc+(width/2);
	}
	public int getX() {
		return xLoc;
	}
	public int getY() {
		return yLoc;
	}
	public void tick(){
		if(runningLeft){
			if(!m.isLeftCollision(left)){
				xLoc-=width/10;
			}
		}else if(runningRight){
			if(!m.isRightCollision(right)){
				xLoc+=width/10;
			}
		}
		if(!jumping){
			for (int i=0; i<height/5; i++) {
				Rectangle2D.Double newBottom = new Rectangle2D.Double(bottom.getMinX(), bottom.getMinY() + 1, bottom.getWidth(), bottom.getHeight());
				if(!m.isBottomCollision(newBottom)){
					falling = true;
					yLoc++;
				} else {
					break;
				}
			}
		}
		if(jumping){
		}
		updateRects();
	}
	private void updateRects(){
		top.setRect(xLoc,yLoc,this.width,1);
		left.setRect(xLoc,yLoc+1,1,this.height-2);
		right.setRect(xLoc+this.width-1,yLoc+1,1,this.height-2);
		bottom.setRect(xLoc,yLoc+height-2,this.width,1);
	}
	public void passPressedKeys(KeyEvent e){
		switch(e.getKeyCode()){
		case KeyEvent.VK_A: runningLeft = true; break;
		case KeyEvent.VK_D: runningRight = true; break;
		case KeyEvent.VK_W: jumping = true; break;
		}
	}
	public void passReleasedKeys(KeyEvent e){
		switch(e.getKeyCode()){
		case KeyEvent.VK_A: runningLeft = false; break;
		case KeyEvent.VK_D: runningRight = false; break;
		case KeyEvent.VK_W: jumping = false; break;
		}
	}
	public void setPosition(Point p) {
		xLoc = p.x;
		yLoc = p.y;
	}
}